  <template>
  <section>
    <!--搜索栏-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item label>
          <el-select
            v-model="filters.AR_number"
            placeholder="区域名称"
            @change="areaChange(filters.AR_number)"
            clearable
          >
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label>
          <el-select v-model="filters.SSL_number" placeholder="宿舍楼名称" clearable>
            <el-option
              v-for="item in dorm"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.SSL_admin" placeholder="宿管员名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" v-on:click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--操作栏-->
    <el-col>
      <el-form :inline="true">
        <el-form-item>
          <el-button type="primary" @click="newFormVisible = true" size="small">新增</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="success" @click="exportExcel = true" size="small">导出至Excel</el-button>
        </el-form-item>-->
      </el-form>
    </el-col>
    <!--展示界面-->
    <el-table
      :data="tableData"
      style="width:100%"
      @selection-change="selsChange"
      v-loading="Loading"
    >
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50" ref></el-table-column>
      <el-table-column align="center" label="区域名称" prop="AR_name" fixed min-width="100"></el-table-column>
      <el-table-column align="center" label="区域编号" prop="AR_number" fixed min-width="80"></el-table-column>
      <el-table-column align="center" label="宿舍楼编号" prop="SSL_number" fixed min-width="90"></el-table-column>
      <el-table-column align="center" label="宿舍楼名称" prop="SSL_name" min-width="150"></el-table-column>
      <el-table-column align="center" label="地理位置" prop="SSL_location" min-width="150"></el-table-column>
      <el-table-column align="center" label="创建人员" prop="SSL_cname" min-width="100"></el-table-column>
      <el-table-column align="center" label="创建时间" prop="SSL_cdate" min-width="100"></el-table-column>
      <el-table-column align="center" label="楼层数" prop="SSL_floor" min-width="80"></el-table-column>
      <el-table-column align="center" label="宿管员" prop="SSL_admin" min-width="100"></el-table-column>
      <el-table-column align="center" label="入住人数" prop="SSL_quantity" min-width="80"></el-table-column>
      <el-table-column align="center" label="备注" prop="SSL_remarks" min-width="200"></el-table-column>
      <el-table-column align="center" label="操作" fixed="right" min-width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>

    <!-- 新增界面 -->
    <el-dialog title="新建宿舍楼" :visible.sync="newFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="newForm" :model="dorm_insert" :rules="rule">
        <el-form-item label="所属区域">
          <el-select v-model="dorm_insert.AR_number" prop="AR_number" placeholder="区域名称">
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍楼名称" prop="SSL_name">
          <el-input v-model="dorm_insert.SSL_name"></el-input>
        </el-form-item>
        <el-form-item label="楼层数" prop="SSL_floor">
          <el-input v-model="dorm_insert.SSL_floor"></el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="SSL_cname">
          <el-input v-model="dorm_insert.SSL_cname"></el-input>
        </el-form-item>
        <el-form-item label="宿管员">
          <el-input v-model="dorm_insert.SSL_admin"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="dorm_insert.SSL_remarks"></el-input>
        </el-form-item>
        <el-form-item label="地理位置">
          <el-input v-model="dorm_insert.SSL_location"></el-input>
        </el-form-item>
        <el-form-item label="人数">
          <el-input v-model="dorm_insert.SSL_quantity"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="newFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="newSubmit('newForm')" :loading="newLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 编辑界面 -->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="editForm" :model="dorm_update" :rules="rule">
        <el-form-item label="区域名称">
          <el-input v-model="dorm_update.AR_name" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="区域编号">
          <el-input v-model="dorm_update.AR_number" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="宿舍楼名称" prop="SSL_name">
          <el-input v-model="dorm_update.SSL_name"></el-input>
        </el-form-item>
        <el-form-item label="宿舍楼编号">
          <el-input v-model="dorm_update.SSL_number" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="宿舍楼位置" prop="SSL_location">
          <el-input v-model="dorm_update.SSL_location"></el-input>
        </el-form-item>
        <el-form-item label="宿舍楼层数" prop="SSL_floor">
          <el-input v-model="dorm_update.SSL_floor"></el-input>
        </el-form-item>
        <el-form-item label="创建人">
          <el-input v-model="dorm_update.SSL_cname" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="创建日期">
          <el-input v-model="dorm_update.SSL_cdate" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="宿舍楼备注" poro="SSL_remarks">
          <el-input v-model="dorm_update.SSL_remarks"></el-input>
        </el-form-item>
        <el-form-item label="宿舍楼管理员" prop="SSL_admin">
          <el-input v-model="dorm_update.SSL_admin"></el-input>
        </el-form-item>
        <el-form-item label="宿舍楼人数" prop="SSL_quantity">
          <el-input v-model="dorm_update.SSL_quantity"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit('editForm')" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template> 
<script>
import { area_select } from "../../api/area.js";
import {
  building_select,
  dorm_update,
  dorm_insert,
  dorm_delete,
} from "../../api/floor.js";
export default {
  data() {
    return {
      filters: {
        AR_number: "",
        SSL_number: "",
        SSL_admin: "",
        page: 1,
      },
      area: [],
      dorm: [],
      rule: {
        area: [
          { required: true, message: "请选择宿舍楼区域", trigger: "change" },
        ],
        dormName: [
          { required: true, message: "请输入宿舍楼名称", trigger: "blur" },
        ],
        layers: [
          { required: true, message: "请输入宿舍楼层数", trigger: "blur" },
          { type: "number", message: "值必须为数字值" },
        ],
      },
      // options: [
      //   {
      //     value: "335",
      //     label: "高松公司北面"
      //   },
      //   {
      //     value: "3109",
      //     label: "高松新能源宿舍楼"
      //   },
      //   {
      //     value: "554",
      //     label: "测试"
      //   }
      // ],

      tableData: [
        {
          AR_name: null,
          AR_number: null,
          SSL_number: null,
          SSL_name: null,
          SSL_cname: null,
          SSL_cdate: null,
          SSL_floor: null,
          SSL_admin: null,
          SSL_quantity: null,
          SSL_remarks: null,
        },
      ],

      dorm_insert: {
        AR_number: null,
        SSL_name: null,
        SSL_floor: null,
        SSL_cname: null,
        SSL_admin: null,
        SSL_remarks: null,
        SSL_quantity: null,
        SSL_location: null,
      },
      dorm_update: {
        AR_name: null,
        AR_number: null,
        SSL_name: null,
        SSL_number: null,
        SSL_location: null,
        SSL_floor: null,
        SSL_cname: null,
        SSL_cdate: null,
        SSL_remarks: null,
        SSL_admin: null,
        SSL_quantity: null,
      },
      // dorm_delete: {
      //   AR_number: null,
      //   SSL_number: null
      // },
      newFormVisible: false,
      alterFormVisible: false,
      Loading: false,
      currentPage: 1,
      totalPage: 1,
      editLoading: false,
      newLoading: false,
    };
  },
  methods: {
    //获取数据
    getData() {
      this.Loading = true;
      building_select(this.filters).then((res) => {
        this.Loading = false;
        console.log(res.data);
        if (res) {
          this.tableData = res.data;
          this.totalPage = res.page;
        } else {
          this.tableData = [];
          this.totalPage = 0;
          this.currentPage = 0;
        }
      });
    },
    //查询
    search() {
      this.filters.page = 1;
      this.getData();
    },
    // 分页
    handleCurrentChange(val) {
      this.filters.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    // 点击修改
    handleEdit(index, row) {
      this.alterFormVisible = true;
      this.dorm_update = Object.assign({}, row);
    },
    //确认修改
    editSubmit(forName) {
      this.$refs[forName].validate((val) => {
        this.editLoading = true;
        dorm_update(this.dorm_update).then((res) => {
          this.editLoading = false;
          if (res) {
            this.$alert(res.msg, "操作提示", {
              confirmButtonTextL: "确定",
              callback: (action) => {
                this.getData();
                this.alterFormVisible = false;
              },
            });
          }
        });
      });
    },
    getArea() {
      area_select({ AR_number: "", AR_name: "", page: 1 }).then((res) => {
        if (res) {
          for (let index = 0; index < res.data.length; index++) {
            this.area.push({
              label: res.data[index].AR_name,
              value: res.data[index].AR_number,
            });
          }
        }
      });
    },
    //获取区域对应宿舍楼
    areaChange() {
      if (this.filters.AR_number === "") {
        return true;
      }
      building_select({
        AR_number: this.filters.AR_number,
        SSL_number: "",
        SSL_admin: "",
        page: 1,
      }).then((res) => {
        if (res) {
          this.dorm = [];
          for (let index = 0; index < res.data.length; index++) {
            this.dorm.push({
              label: res.data[index].SSL_name,
              value: res.data[index].SSL_number,
            });
          }
        } else {
          this.dorm = [
            {
              value: "",
              label: "",
            },
          ];
          this.filters._dorm = "";
        }
      });
    },
    //新增宿舍楼
    newSubmit(forName) {
      this.$refs[forName].validate((val) => {
        if (val) {
          this.newLoading = true;
          var user = sessionStorage.getItem("user");
          // console.dir(user);
          // this.dorm_insert.AR_createman = user;
          dorm_insert(this.dorm_insert).then((res) => {
            this.newLoading = false;
            if (res) {
              this.$alert(res.msg, "操作提示", {
                confirmButtonTextL: "确定",
                callback: (action) => {
                  this.getData();
                  this.newFormVisible = false;
                  this.dorm_insert = {
                    AR_number: null,
                    SSL_name: null,
                    SSL_floor: null,
                    SSL_cname: null,
                    SSL_remarks: null,
                    SSL_admin: null,
                    SSL_quantity: null,
                  };
                },
              });
            }
          });
        }
      });
    },
    //当选择项发生变化时会触发该事件
    selsChange: function (sels) {
      this.sels = sels;
    },
    //删除宿舍楼
    handleDel(row) {
      let AR_number = row.AR_number;
      let SSL_number = row.SSL_number;
      // console.dir(res);
      // 提示是否删除
      this.$confirm("此操作将永久删除该宿舍楼下所有内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 进行删除操作
          this.Loading = true;
          // console.log(AR_number, SSL_number);
          dorm_delete(AR_number, SSL_number).then((res) => {
            this.Loading = false;
            if (res) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.search();
    this.getArea();
  },
};
</script>